﻿package edu.mit.media.fv.components
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
    
	import edu.mit.media.fv.blocks.*;
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	import edu.mit.media.fv.aqwidgets.*;

	public class AQPollOption extends MovieClip
	{
		var _label:AQTextField;
		var _recorder:AQRecorder;
        var _wall:Sprite;
		
		var _checkBox:AQCheckBox;
		var _radioButton:AQRadioButton;
        
        public var optionID:int;
		private var recordable:Boolean;
		
		public function AQPollOption(optionID:int=-1, text:String='', recordable:Boolean=false)
		{
			super();
			_checkBox = new AQCheckBox();
			_checkBox.x  = 15;
			_checkBox.y = 10;
			//this.addChild(this._checkBox);
			
			_radioButton = new AQRadioButton();
			_radioButton.x = 25;
			_radioButton.y = 10;
			_radioButton.selected = false;
			//this.addChild(this._radioButton);
			
			_wall = new Sprite();
			_wall.graphics.beginFill(0x333333, 0.10);
			_wall.graphics.lineStyle(1, 0xFFFFFF, 0.50);
			_wall.graphics.drawRoundRect(0,0,550,100,5,5);
			_wall.graphics.endFill();
			this.addChildAt(_wall, 0);
			
			this.addEventListener(MouseEvent.CLICK, processMouseClicked);
            if (optionID == -1) {
                this.optionID = Math.floor(Math.random()*1000000);
            } else {
                this.optionID = optionID;
            }
            
            if (text != '') {
                this.label.text = text;
            }
            
			if (recordable)
			{
				this.recorder.addEventListener(AQRecorderEvent.START_RECORD, pressStartRecord);
				this.recorder.addEventListener(AQRecorderEvent.STOP_RECORD, pressStopRecord);
			}
            this.recorder.addEventListener(AQRecorderEvent.START_PLAY, pressStartPlay);
            this.recorder.addEventListener(AQRecorderEvent.STOP_PLAY, pressStopPlay);
		}
        
		public function processMouseClicked(event:MouseEvent):void{
			this.dispatchEvent(new AQPollOptionEvent(AQPollOptionEvent.POLL_SELECTED, this));
		}
		public function focusOption(o:Boolean):void{
			if(o){
				_wall.graphics.clear();
				_wall.graphics.beginFill(0x333333);
				_wall.graphics.lineStyle(1, 0xFFFFFF, 0.50);
				_wall.graphics.drawRoundRect(0,0,550,100,5,5);
				_wall.graphics.endFill();
			}else{
				_wall.graphics.clear();
				_wall.graphics.beginFill(0x333333, 0.10);
				_wall.graphics.lineStyle(1, 0xFFFFFF, 0.50);
				_wall.graphics.drawRoundRect(0,0,550,100,5,5);
				_wall.graphics.endFill();
			}
		}
		public function isCheckboxSelected():Boolean{
			return this._checkBox.isSelected();
		}
		public function isRadioButtonSelected():Boolean{
			return this._radioButton.selected;
		}
		public function selectRadioButton(o:Boolean):void{
			this._radioButton.selected = o;
		}
		public function includeCheckBox():void{
			this.addChild(this._checkBox);
		}
		public function includeRadioButton():void{
			//_radioButton.selected = false;
			this.addChild(this._radioButton);
		}
		public function get text():String{
			return this._label.text;
		}
		public function set text(t:String):void{
			trace("setting text on: "+this.label+" with text: "+t);
			this.label.text = t;
		}
		public function set editable(o:Boolean):void{
			this._label.editable = o;
			//this._recorder.includeRecordButton(false);
		}
		
        public function pressStartRecord(event:AQRecorderEvent):void{
            trace('poll start record');
			var data:Object = new Object();
			data.command = FluidConstants.POLL_START_RECORD;
			data.id = this.optionID;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
		}
		public function pressStopRecord(event:AQRecorderEvent):void{
            trace('poll stop record');
			var data:Object = new Object();
			data = new Object();
			data.command = FluidConstants.POLL_STOP_RECORD;
            data.id = this.optionID;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
		}
		public function pressStartPlay(event:AQRecorderEvent):void{
            trace('poll start play');
			var data:Object = new Object();
			data.command = FluidConstants.POLL_START_PLAY;
            data.id = this.optionID;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
		}
		public function pressStopPlay(event:AQRecorderEvent):void{
            trace('poll stop play');
			var data:Object = new Object();
			data.command = FluidConstants.POLL_STOP_PLAY;
            data.id = this.optionID;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
		}
        
        public function getRepresentation():Object {
            var rep:Object = new Object();
            rep.id = this.optionID;
            rep.text = this._label.text;
            return rep
        }
	}
}